package com.zfy.cmsservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zfy.cmsservice.entity.Classes;
import com.zfy.cmsservice.entity.Students;
import com.zfy.cmsservice.service.StudentsService;
import com.zfy.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-11-18
 */
@RestController
@RequestMapping("/cmsservice/students")
@Api(description = "学生管理-学生信息维护")
public class StudentsController {
    @Autowired
    private StudentsService studentsService;

    // 分页查询学生信息 支持根据名称 性别检索
    @ApiOperation(value = "分页查询学生")
    @PostMapping("pageStudents/{page}/{size}")
    public R pageStudents(@PathVariable long page, @PathVariable long size,
                           @RequestParam(required = false) String name,
                          @RequestParam(required = false) Integer gender) {
        return studentsService.pageCondition(page,size,name,gender);
    }


//    添加学生信息
    @ApiOperation(value = "添加学生信息")
    @PostMapping("addStudents")
    public R addStudents(@RequestBody Students students){
        boolean flag = studentsService.save(students);
        if(flag) {
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }
//    修改学生信息
    @ApiOperation(value = "修改学生信息")
    @PostMapping("updateStudents")
    public R updateStudents(@RequestBody Students students) {
        if (StringUtils.isEmpty(students)) {
            // 传参错误异常
            return R.httpError();
        }
        boolean flag = studentsService.updateById(students);
        if(flag) {
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    // 逻辑删除学生信息
    @ApiOperation(value = "逻辑删除学生信息")
    @PostMapping("removeStudents/{id}")
    public R removeStudents(@PathVariable String id){
        boolean remove = studentsService.removeById(id);
        if(remove) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    //根据id查询学生
    @ApiOperation(value = "根据id查询学生")
    @GetMapping("getStudents/{id}")
    public R getClassById(@PathVariable String id) {
        Students byId = studentsService.getById(id);
        return R.ok().data("item", byId);
    }

//    根据班级id 和学生姓名查询学生
    @ApiOperation(value = "根据班级id 和学生姓名查询学生")
    @GetMapping("getStudentsName")
    public R getStudentsName(@RequestParam String classId,
                             @RequestParam(required = false) String name) {
        return studentsService.getStudentsName(classId,name);
    }

//    根据身份证号查询学生
    @ApiOperation(value = "根据身份证号查询学生id")
    @GetMapping("getStudentsId")
    public R getStudentsId(@RequestParam String idCard) {
        QueryWrapper<Students> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_card", idCard).select("id");
        Students student = studentsService.getOne(queryWrapper);

        // 处理查询结果
        if (student != null) {
            String studentId = student.getId();
            String name = student.getName();
            // 返回学生的 id
            return R.ok().data("studentId", studentId).data("studentName",name);
        } else {
            // 没有找到匹配的学生记录
            return R.error().message("没有找到匹配的学生记录");
        }
    }


}

